home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / MetalworksHelp.java < prev    next >
Text File  |  1998-06-30  |  4KB  |  140 lines

  1. /*
  2.  * @(#)MetalworksHelp.java    1.3 98/04/12
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. import com.sun.java.swing.*;
  22. import java.awt.*;
  23. import java.net.URL;
  24. import java.net.MalformedURLException;
  25. import java.io.*;
  26. import com.sun.java.swing.text.*;
  27. import com.sun.java.swing.event.*;
  28.  
  29. /*
  30.  * @version 1.3 04/12/98
  31.  * @author Steve Wilson
  32.  */
  33. public class MetalworksHelp extends JInternalFrame {
  34.   
  35.     public MetalworksHelp() {
  36.     super("Help", true, true, true, true);
  37.  
  38.            setFrameIcon( (Icon)UIManager.get("Tree.openIcon")); // PENDING(steve) need more general palce to get this icon
  39.     setBounds( 200, 25, 400, 400);
  40.     HtmlPane html = new HtmlPane();
  41.     setContentPane(html);
  42.     }
  43.  
  44. }
  45.  
  46.  
  47. class HtmlPane extends JScrollPane implements HyperlinkListener {
  48.     JEditorPane html;
  49.  
  50.     public HtmlPane() {
  51.     try {
  52.         File f = new File ("HelpFiles/toc.html");
  53.         String s = f.getAbsolutePath();
  54.         s = "file:"+s;
  55.         URL url = new URL(s);
  56.         html = new JEditorPane(s);
  57.         html.setEditable(false);
  58.         html.addHyperlinkListener(this);
  59.  
  60.         JViewport vp = getViewport();
  61.         vp.add(html);
  62.     } catch (MalformedURLException e) {
  63.         System.out.println("Malformed URL: " + e);
  64.     } catch (IOException e) {
  65.         System.out.println("IOException: " + e);
  66.     }    
  67.     }
  68.  
  69.     /**
  70.      * Notification of a change relative to a 
  71.      * hyperlink.
  72.      */
  73.     public void hyperlinkUpdate(HyperlinkEvent e) {
  74.     if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
  75.         linkActivated(e.getURL());
  76.     }
  77.     }
  78.  
  79.     /**
  80.      * Follows the reference in an
  81.      * link.  The given url is the requested reference.
  82.      * By default this calls <a href="#setPage">setPage</a>,
  83.      * and if an exception is thrown the original previous
  84.      * document is restored and a beep sounded.  If an 
  85.      * attempt was made to follow a link, but it represented
  86.      * a malformed url, this method will be called with a
  87.      * null argument.
  88.      *
  89.      * @param u the URL to follow
  90.      */
  91.     protected void linkActivated(URL u) {
  92.     Cursor c = html.getCursor();
  93.     Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
  94.     html.setCursor(waitCursor);
  95.     SwingUtilities.invokeLater(new PageLoader(u, c));
  96.     }
  97.  
  98.     /**
  99.      * temporary class that loads synchronously (although
  100.      * later than the request so that a cursor change
  101.      * can be done).
  102.      */
  103.     class PageLoader implements Runnable {
  104.     
  105.     PageLoader(URL u, Cursor c) {
  106.         url = u;
  107.         cursor = c;
  108.     }
  109.  
  110.         public void run() {
  111.         if (url == null) {
  112.         // restore the original cursor
  113.         html.setCursor(cursor);
  114.  
  115.         // PENDING(prinz) remove this hack when 
  116.         // automatic validation is activated.
  117.         Container parent = html.getParent();
  118.         parent.repaint();
  119.         } else {
  120.         Document doc = html.getDocument();
  121.         try {
  122.             html.setPage(url);
  123.         } catch (IOException ioe) {
  124.             html.setDocument(doc);
  125.             getToolkit().beep();
  126.         } finally {
  127.             // schedule the cursor to revert after
  128.             // the paint has happended.
  129.             url = null;
  130.             SwingUtilities.invokeLater(this);
  131.         }
  132.         }
  133.     }
  134.  
  135.     URL url;
  136.     Cursor cursor;
  137.     }
  138.  
  139. }
  140.